<template>
  <div class="blog-column">
    <div class="column-header">
      <div class="column-img-box">
        <img v-if="!columnMes.img" src="~@/assets/img/column/default.png" />
        <img v-if="columnMes.img" :src="columnMes.img" />
      </div>
      <div class="column-message-box">
        <h2>{{ columnMes.name }}</h2>
        <p>{{ columnMes.description }}</p>
        <span>文章数 {{ total }}</span>
      </div>
    </div>
    <div class="column-welcome">欢迎大家来到小新的个人空间! 每一次分享都是自己的提高</div>
    <el-divider></el-divider>
    <div class="column-content" ref="column">
      <BlogItem :list="blogList" />
      <LoadingMore
        :loadingStatus="loadingStatus"
        :pageSize="pageSize"
        :toLoadHeight="toLoadHeight"
        :ajaxFun="loadingMore"
        :scrollDom="scrollDom"
      />
    </div>
  </div>
</template>

<script>
export default {
  created() {
    this.columnId = this.$route.params.id
    this.getData()
  },
  mounted() {
    this.scrollDom = this.$refs['column']
  },
  beforeRouteUpdate(to, from, next) {
    this.blogList = []
    this.columnId = to.params.id
    this.getData()
    next()
  },
  data() {
    return {
      columnId: undefined,
      total: 0,
      blogList: [],
      columnMes: {},
      loadingStatus: 1,
      // 总页数
      pageSize: 4,
      toLoadHeight: 680,
      scrollDom: {},
      params: {
        page: 1,
        limit: 5
      }
    }
  },
  methods: {
    getData() {
      this.$ajax({
        url: this.$ajax.setUrl(`/column/${this.columnId}`),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.columnMes = data.data
          this.blogList.push(...data.data.list)
          if (this.params.limit >= data.data.total) {
            this.loadingStatus = 3
          }
          this.total = data.data.total
          this.pageSize = Math.ceil(data.data.total / this.params.limit)
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    loadingMore(curPage) {
      if (this.loadingStatus === 3) {
        return
      }
      this.loadingStatus = 2

      this.loadingStatus = this.pageSize > curPage ? 1 : 3
      this.params.page++
      this.getData()
    }
  }
}
</script>

<style lang="scss">
.blog-column {
  padding: 20px;
  background: #fff;
  border-radius: 10px;
  .column-header {
    display: flex;
    .column-img-box {
      margin-right: 20px;
      width: 112px;
      height: 112px;
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 5px;
      }
    }
    .column-message-box {
      width: 80%;
      h2,
      p,
      span {
        display: block;
      }
      h2 {
        font-size: 20px;
        color: #222226;
        height: 28px;
        line-height: 28px;
        font-weight: 600;
        white-space: normal;
      }
      p {
        width: 100%;
        height: 48px;
        margin: 5px 0;
        // border: 1px solid black;
        letter-spacing: 1px;
        overflow: hidden;

        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        word-break: break-all;
      }
      span {
        font-size: 14px;
        color: #919da9;
        letter-spacing: 0;
        line-height: 22px;
      }
    }
  }

  .column-welcome {
    background-color: #f9fafb;
    border-radius: 2px;
    padding: 12px 16px;
    margin: 20px 0;
    color: #919da9;
    letter-spacing: 2px;
  }
}
</style>
